package com.syf.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.domain.store.BsSoftwareRelease;
import com.syf.model.Mails;
import com.syf.rocker.Source;
import com.syf.service.BsBigClassService;
import com.syf.service.BsSmallClassService;
import com.syf.service.BsSoftwareReleaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.domain.store.BsSoftwareIncluded;
import com.syf.mapper.BsSoftwareIncludedMapper;
import com.syf.service.BsSoftwareIncludedService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MimeTypeUtils;

/**
*
*
*@author syf
*@create 2022-02-07 19:09
*/
@Service
public class BsSoftwareIncludedServiceImpl extends ServiceImpl<BsSoftwareIncludedMapper, BsSoftwareIncluded> implements BsSoftwareIncludedService{

    @Autowired
    private BsSoftwareReleaseService softwareReleaseService;

    @Autowired
    private BsBigClassService bsBigClassService;

    @Autowired
    private BsSmallClassService bsSmallClassService;

    @Autowired
    private Source source;

    /**
     * 审核发布应用:
     *
     * @param bsSoftwareIncluded
     * @return
     */
    @Override
    @Transactional
    public boolean auditSoftware(BsSoftwareIncluded bsSoftwareIncluded) {
        Integer num = RandomUtil.randomInt(1,99999999);
        bsSoftwareIncluded.setId(num);
        boolean save = save(bsSoftwareIncluded);


        if(save){
            Mails mails = null;

            BsSoftwareRelease bsSoftwareRelease = softwareReleaseService.selBySign(bsSoftwareIncluded.getSoftwareSign());
            bsSoftwareRelease.setAuditId(num);
            if(bsSoftwareIncluded.getStatus() == 0){  //审核通过
                bsSoftwareRelease.setSoftwareCode(bsSoftwareRelease.getSoftwareSign());
                bsSoftwareRelease.setReleaseStatus(1);  //申请单状态--成功
                bsSoftwareRelease.setStatus(0);  //应用状态--在线
                bsSoftwareRelease.setDownloadSize(0);
                /*String smallClassName = bsSmallClassService.getById(bsSoftwareRelease.getSmallClass()).getSmallClassName();
                bsSoftwareRelease.setToCategory(smallClassName);  //所属分类*/

                //使用MQ异步发生邮箱给厂商
                mails = new Mails("1548120092@qq.com",
                        "贵司发布的软件"+ bsSoftwareIncluded.getSoftwareName()+"未通过审核",
                        "软件不达标");

            }else {
                //使用MQ异步发生邮箱给厂商
                mails = new Mails("1548120092@qq.com",
                        "贵司发布的软件"+ bsSoftwareIncluded.getSoftwareName()+"通过了审核",
                        "软件非常优秀");
                bsSoftwareRelease.setReleaseStatus(2);
            }

            MessageBuilder<Mails> mailMessageBuilder = MessageBuilder
                    .withPayload(mails)
                    .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
            boolean send = source.mailOut().send(mailMessageBuilder.build());

            //修改应用发布表状态
            boolean update = softwareReleaseService.updateById(bsSoftwareRelease);
            return update;
        }

        return save;
    }

    /**
     * 分页条件查询--应用审核列表
     * @param page
     * @param companyName
     * @param softwareName
     * @param status
     * @return
     */
    @Override
    public Page<BsSoftwareIncluded> findByPage(Page<BsSoftwareIncluded> page, String companyName, String softwareName, Integer status) {
        LambdaQueryWrapper<BsSoftwareIncluded> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(companyName),BsSoftwareIncluded::getCompanyName,companyName)
                .like(StringUtils.isNotBlank(softwareName),BsSoftwareIncluded::getSoftwareName,softwareName)
                .eq(status !=null, BsSoftwareIncluded::getStatus,status)
                .orderByDesc(BsSoftwareIncluded::getCreateTime);
        Page<BsSoftwareIncluded> includedPage = page(page, wrapper);
        return includedPage;
    }
}
